package org.come.MountShouHu;

import com.tool.btn.MountShouhuBtn;
import com.tool.tcpimg.UIUtils;
import org.come.bean.QualityClBean;
import org.come.entity.Goodstable;
import org.come.until.GoodsListFromServerUntil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ShouhuPackJpanel extends JPanel {
    private ImageIcon ico=new ImageIcon("inkImg/Client/守护石物品栏.png");
    private  JLabel[] GoodsListLabel=new JLabel[12];
    private ShouhuPackGoodsMouslisten[] goodsMouslistens=new ShouhuPackGoodsMouslisten[12];
    private static JLabel effect;
    private static ImageIcon iconx;
    private static int xz;
    public int page=0;
    private static ImageIcon[] goodimg=new ImageIcon[12];
    private int idx=-1;
    private List<Goodstable>goodstableList=new ArrayList<>();
    private int xuanzhong1=-1;
    private int xuanzhong2=-1;
    public List<Goodstable>Eqment=new ArrayList<>();
    public MountShouhuBtn up;
public MountShouhuBtn down;
    public ShouhuPackJpanel(){
        this.setPreferredSize(new Dimension(339, 150));
        this.setLayout(null);
        this.setBackground(UIUtils.Color_BACK);
       int i = 0;
        while (i < 12) {
            final int row = i % 6 * 51;
            final int col = i / 6 * 51;
            final int[] arr = { 10 + row+2+6 , 10 + col+1+6};
            GoodsListLabel[i] = new JLabel();
            this.goodsMouslistens[i] = new ShouhuPackGoodsMouslisten(i, this, arr);
            GoodsListLabel[i].addMouseListener((MouseListener)this.goodsMouslistens[i]);
            GoodsListLabel[i].setBounds(10 + row + 2 + 6, 10 + col + 1 + 6, 50, 50);
            this.add((Component)GoodsListLabel[i]);
            ++i;
        }
        this.add(effect);
        up=new MountShouhuBtn("inkImg/Client/向右箭头11X15.png",1,70,this);
        up.setBounds(35,120,17,15);
        this.add(up);
        down=new MountShouhuBtn("inkImg/Client/向左箭头11X15.png",1,71,this);
        down.setBounds(20,120,17,15);
        this.add(down);
    }
    public void init(int idx){
        for (int k=0;k<=goodimg.length-1;k++){
            goodimg[k]=null;
        }
        getGoodstableList().removeIf(Objects::isNull);
        getGoodstableList().removeIf(e->e.getUsetime()==0);
    
        for (int i = page * 12; i<=getGoodstableList().size()-1; i++){
            {
                if (getGoodstableList().get(i)!=null&&i-12*page<12){
                    goodimg[i-12*page]=GoodsListFromServerUntil.imgpath(getGoodstableList().get(i).getSkin());
                }
            
            }
        
        }
     if (idx!=-1)
     this.idx=idx;
    }
    public void  updata(){
        for (int k=0;k<=goodimg.length-1;k++){
            goodimg[k]=null;
        }
        getGoodstableList().removeIf(Objects::isNull);
        getGoodstableList().removeIf(e->e.getUsetime()==0);
    
        for (int i = page * 12; i<=getGoodstableList().size()-1; i++){
            {
                if (getGoodstableList().get(i)!=null&&i-12*page<12){
                    goodimg[i-12*page]=GoodsListFromServerUntil.imgpath(getGoodstableList().get(i).getSkin());
                }
            
            }
        
        }
    }
    public void xuanzhong( final int shopPlace) {
        Goodstable goodstable=null;
        if (getGoodstableList().size()!=0&&shopPlace+page*12<getGoodstableList().size())
         goodstable=goodstableList.get(shopPlace+page*12);
        if (goodstable == null) {
            if (xz != -1) {
                GoodsListLabel[xz].setBorder(null);
            }
            xz = -1;
        }
        else {
            if (xz != -1) {
                GoodsListLabel[xz].setBorder(null);
            }
            xz = shopPlace;
            GoodsListLabel[xz].setBorder(BorderFactory.createLineBorder(Color.red));
        }

        init(idx);
    }
  public void ew(QualityClBean clBean){
      for (Goodstable goodstable:getGoodstableList()){
          if (goodstable!=null&&goodstable.getRgid().longValue()==clBean.getRgid().longValue()){
              goodstable.setValue(clBean.getNewAttr());
              RandFJframe.getRandFJframe().getRandFJpanel().RL1=null;
              RandFJframe.getRandFJframe().getRandFJpanel().RL2=null;
              RandFJframe.getRandFJframe().getRandFJpanel().FL=null;
              break;
          }
      }
      updata();
    for (int i=0;i<= getGoodstableList().size()-1;i++){
        if (getGoodstableList().get(i).getRgid().longValue()==clBean.getRgid().longValue()){
            xuanzhong1=i;
            break;
        }
    }
      init(1);
  }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (ico!=null)
            g.drawImage(ico.getImage(),0,0,339,150,null);
        for (int i=0;i<12;i++){
            final int row = i % 6 * 51;
            final int col = i / 6 * 51;
            if (goodimg[i]!=null){
                g.drawImage(goodimg[i].getImage(), 10 + row + 2 + 6, 10 + col + 1 + 6, 45, 45, null);
                if (getGoodstableList().get(i).getGoodlock()==1)
                g.drawImage(GoodsListFromServerUntil.lockimg.getImage(), 10 + row + 2 + 6, 10 + col + 1 + 6, 10, 12, null);
            }
           
        }
        g.setColor(Color.black);
        g.setFont(UIUtils.TEXT_NAME_FONT);
      g.drawString(page+"/4",60,135);
      
    }

    public List<Goodstable> getGoodstableList() {
        return goodstableList;
    }

    public void setGoodstableList(List<Goodstable> goodstableList) {
        this.goodstableList = goodstableList;
    }

    public static JLabel getEffect() {
        return effect;
    }

    public static void setEffect(JLabel effect) {
        ShouhuPackJpanel.effect = effect;
    }

    public int getIdx() {
        return idx;
    }

    public void setIdx(int idx) {
        this.idx = idx;
    }

    public void effect(int place){
        if (place!=-1){
            effect.setBounds(GoodsListLabel[place].getX(),GoodsListLabel[place].getY(),50,50);
        }else  effect.setBounds(1000,1000,52,52);

}

    public int getXuanzhong1() {
        return xuanzhong1;
    }

    public void setXuanzhong1(int xuanzhong1) {
        if (xuanzhong1!=-1)
        if (RandFJframe.getRandFJframe().getRandFJpanel().type==2){
            RandFJframe.getRandFJframe().getRandFJpanel().RL1=goodstableList.get(xuanzhong1);
           
        }
        this.xuanzhong1 = xuanzhong1;
    }

    public int getXuanzhong2() {
        return xuanzhong2;
    }

    public void setXuanzhong2(int xuanzhong2) {
        if (xuanzhong2!=-1)
        RandFJframe.getRandFJframe().getRandFJpanel().RL2=goodstableList.get(xuanzhong2);
        this.xuanzhong2 = xuanzhong2;
    }

    static {
        effect = new JLabel() {
            @Override
            protected void paintComponent(final Graphics g) {
                super.paintComponent(g);
                g.drawImage(iconx.getImage(), 0, 0, 50, 50, (ImageObserver)this);
            }
        };
        iconx = new ImageIcon("inkImg/background/border_quack.png");
        xz = -1;
    }
}
